package com.example.wto.viewpagertest01;

import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

public class UtilClassRoomSql implements IUtilSql
{
		private UtilSql utilSql=null;
		public UtilClassRoomSql(UtilSqlHelper sqlHelper)
		{
			utilSql=new UtilSql(sqlHelper);
		}
	    public void insertClassRoom(ClassRoom classRoom)
	    {
	    	ClassRoomBean bean=classRoom.getBean();
	    
	        String sql="insert into class_room_table (college_id,college_name,s_id,s_name,campus_id,campus_name,teach_build_id,teach_build_name,class_room_id,class_room_name) values(?,?,?,?,?,?,?,?,?,?)";
	        List<Object> params=new ArrayList<Object>();
	        params.add(bean.getCollegeID());
	        params.add(bean.getCollegeName());
	        params.add(bean.getS_id());
	        params.add(bean.getS_name());
	        params.add(bean.getCampusID());
	        params.add(bean.getCampusName());
	        params.add(bean.getTeachBuildID());
	        params.add(bean.getTeachBuildName());
	        params.add(bean.getClassRoomID());
	        params.add(bean.getClassRoomName());
	        
	        utilSql.update(sql,params);
	        List<DayOfWeek> weekList=classRoom.getWeekList();
	        
	        for(int i=0;i<weekList.size();i++)
	        {
	        	params.clear();
	        	params.add(bean.getCollegeID());
	 	        params.add(bean.getS_id());
	 	        params.add(bean.getCampusID());	  
	 	        params.add(bean.getTeachBuildID());	 	  
	 	        params.add(bean.getClassRoomID());
	 	  
	           
	            List<Course> courseList=weekList.get(i).getCourseList();
	            String sql_one="insert into week_class_room_table values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
	            params.add(weekList.get(i).getWeekName());
	            params.add(i+"");
	            for(int j=0;j<courseList.size();j++)
	            {
	                params.add(courseList.get(j).toString());
	            }
	            utilSql.update(sql_one,params);

	        }
	        utilSql.close();
	    }
	    public ClassRoom selectClassRoom(ClassRoomBean bean)
	    {
	        boolean flag=false;
	        String sql="select t.college_id,t.college_name,t.s_id,t.s_name,t.campus_id,t.campus_name,t.teach_build_id,t.teach_build_name,t.class_room_id,t.class_room_name,week_name,order_t,course_one,course_two,course_three,course_four,course_five,course_six from class_room_table t ,week_class_room_table w where t.college_id = w.college_id and t.s_id = w.s_id and t.campus_id=w.campus_id and t.teach_build_id=w.teach_build_id and t.class_room_id=w.class_room_id and t.college_id=? and t.s_id=? and t.campus_id=? and t.teach_build_id=? and t.class_room_id=?  order by order_t;";
	        ClassRoom classRoom=new ClassRoom();
	        Cursor resultSet=utilSql.select(sql,new String[]{bean.getCollegeID(),bean.getS_id(),bean.getCampusID(),bean.getTeachBuildID(),bean.getClassRoomID()});
	        List<DayOfWeek> weekList=new ArrayList<DayOfWeek>();
	        try
			{
	            while(resultSet.moveToNext())
	            {
	            	bean.setCollegeName(resultSet.getString(resultSet.getColumnIndex("college_name")));
	            	bean.setS_name(resultSet.getString(resultSet.getColumnIndex("s_name")));
	            	bean.setCampusName(resultSet.getString(resultSet.getColumnIndex("campus_name")));
	            	bean.setTeachBuildName(resultSet.getString(resultSet.getColumnIndex("teach_build_name")));
	            	bean.setClassRoomName(resultSet.getString(resultSet.getColumnIndex("class_room_name")));
	                
	                List<Course> courses=new ArrayList<Course>();
	                DayOfWeek weekday=new DayOfWeek();
					//获取课程数据
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_one"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_two"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_three"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_four"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_five"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_six"))));
					//设置星期几
					weekday.setWeekName(resultSet.getString(resultSet.getColumnIndex("week_name")));
					//添加课程数据
					weekday.getCourseList().clear();
					weekday.getCourseList().addAll(courses);
					//添加星期数据
					weekList.add(weekday);
					flag=true;
	            }
	           classRoom.setBean(bean);
			}
	        finally
	        {
	        	 utilSql.close();
				 resultSet.close();
	        }
	        if(flag)
	        {
	            classRoom.getWeekList().clear();
	            classRoom.getWeekList().addAll(weekList);
	            return classRoom;
	        }
	        else
	            return null;
	    }
		@Override
		public void insert(Object obj)
		{
			ClassRoom classRoom=(ClassRoom)obj;
			insertClassRoom(classRoom);
		}
		@Override
		public Object select(Object obj)
		{
			ClassRoom classRoom=selectClassRoom((ClassRoomBean)obj);
			return classRoom;
		}
}
